// 接口
import { bannerUrl, personalUrl, sortUrl } from '../../config/api.js'
import { requestData } from '../../config/requestData.js'

Page({
  data: {
    swiperList: [], //轮播图数据
    navList: [
      { icon: 'icon-fangzi', label: '每日推荐'},
      { icon: 'icon-24gf-playlistMusic4', label: '歌曲'},
      { icon: 'icon-zhuzhuangtu', label: '排行榜'},
      { icon: 'icon-yinleyanzou', label: '电台'},
      { icon: 'icon-shexiangji', label: '直播'},
    ],
    recommendList: [], //推荐歌曲数据
    hotList: [], //排行榜数据
  },
  /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
      this.loadingSwiperData()
      this.loadingRecommendData()
      this.loadingHotData()
    },

  //方法
  goRecommend() {
    wx.navigateTo({
      url: '/pages/recommendSong/recommendSong',
    })
  },
  //加载方式
    // 获取轮播图数据
    async loadingSwiperData () {
      const options = {
        url: bannerUrl,
        data: {
          type: 2
        }
      }
      const res = await requestData(options)
      const { banners, code } = res 
      if (code === 200) {
        this.setData({
          swiperList: banners
        })
      }
    },
    // 获取推荐歌曲数据
    async loadingRecommendData () {
      const options = {
        url: personalUrl,
        data: {
          type: 2,
          limit: 20
        }
      }
      const res = await requestData(options)
      const { result, code } = res 
      if (code === 200) {
        this.setData({
          recommendList: result
        })
      }
    },
     // 获取排行榜歌曲数据
    async loadingHotData () {
      let index = 0,
          hotArr = [];
      while(index < 5) {
        const options = {
          url: sortUrl,
          data: {
            idx: index++
          }
        }
        const res = await requestData(options)
        const { playlist, code } = res
         if (code === 200) {
          const { name, tracks } = playlist
          let hotObj = {
            name,
            tracks: tracks.slice(0, 3)
          }
          hotArr.push(hotObj)
          this.setData({
            hotList: hotArr
          })
        }
    }
    },
  // 点击事件
})